package cn.wolfcode.trip.base.domain;

import cn.wolfcode.trip.base.util.JSONUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class Commodity extends BaseDomain{

    //商品名称
    private String name;

    //商品描述
    private String description;

    //商品图片
    private String picture;

    //价格
    private BigDecimal price;

    //商品收藏数
    private GoodsCollection goodsCollection;

    //上架时间
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date shelfTime;

    //商品状态,1表示上架,0表示下架
    private boolean state;

    private CommodityCatalog commodityCatalog;

    public String getJson() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("name", name);
        map.put("picture", picture);
        map.put("description", description);
        map.put("price", price);
        if (shelfTime!=null){
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            String date = s.format(shelfTime);
            map.put("shelfTime", date);
        }
        map.put("state", state);
        if(commodityCatalog!=null){
            map.put("commodityCatalogId", commodityCatalog.getId());
            map.put("commodityCatalogTitle", commodityCatalog.getTitle());
        }
        return JSONUtil.toJSONString(map);
    }

}